//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package cc.rengu.redp.bizimpl.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GZIPUtils {
    private static final Logger log = LoggerFactory.getLogger(GZIPUtils.class);
    public static final String GZIP_ENCODE_UTF_8 = "UTF-8";
    public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";

    public GZIPUtils() {
    }

    public static byte[] compress(String str) {
        return compress(str, "UTF-8");
    }

    public static byte[] compress(String str, String encoding) {
        if (null != str && !str.trim().isEmpty()) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = null;

            try {
                gzip = new GZIPOutputStream(out);
                gzip.write(str.getBytes(encoding));
            } catch (IOException var8) {
                log.error("gzip compress error.", var8);
            } finally {
                IOUtils.closeQuietly(gzip);
            }

            return out.toByteArray();
        } else {
            return null;
        }
    }

    public static byte[] uncompress(byte[] bytes) {
        if (bytes != null && bytes.length != 0) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPInputStream ungzip = null;
            ByteArrayInputStream in = null;

            try {
                in = new ByteArrayInputStream(bytes);
                ungzip = new GZIPInputStream(in);
                byte[] buffer = new byte[256];

                int n;
                while((n = ungzip.read(buffer)) >= 0) {
                    out.write(buffer, 0, n);
                }
            } catch (IOException var9) {
                log.error("gzip uncompress error.", var9);
            } finally {
                IOUtils.closeQuietly(ungzip);
                IOUtils.closeQuietly(in);
            }

            return out.toByteArray();
        } else {
            return null;
        }
    }

    public static String uncompressToString(byte[] bytes) {
        return uncompressToString(bytes, "UTF-8");
    }

    public static String uncompressToString(byte[] bytes, String encoding) {
        if (bytes != null && bytes.length != 0) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteArrayInputStream in = null;
            GZIPInputStream ungzip = null;

            try {
                in = new ByteArrayInputStream(bytes);
                ungzip = new GZIPInputStream(in);
                byte[] buffer = new byte[256];

                int n;
                while((n = ungzip.read(buffer)) >= 0) {
                    out.write(buffer, 0, n);
                }

                String result = out.toString(encoding);
                String var8 = result;
                return var8;
            } catch (IOException var12) {
                log.error("gzip uncompress to string error.", var12);
            } finally {
                IOUtils.closeQuietly(ungzip);
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }

            return null;
        } else {
            return null;
        }
    }

    public static void main(String[] args) {
        String str = "%5B%7B%22lastUpdateTime%22%3A%222011-10-28+9%3A39%3A41%22%2C%22smsList%22%3A%5B%7B%22liveState%22%3A%221";
        str = "10000001,10000002,31002579,31002478,31002353,31005233,31005211,31002720,31002744,31002605,31004801,31004902,31002439,31002872,31005244,31005255,31004724,31004888,31005346,31005391,31002492,31005380,31005153,31005814,31004763,31005698,31002340,310023338,31002365,31002617,31002555,31002807,31004890,31004914,31004751,31002542,31002454,31005379,31005186,31005222,31002858,31002631,31005197,31005175,31005164,31004926,31004712,31004864,31004876,31002504,31005200,31004799,31004736,31004938,31002415,31002338,31002581,31002795,31002629,31004813,31005778,31004748,31004837,31004849,31002516,31002593,31002466,31005266,31002389,31002391,31002302,31002480,31002706,31002718,31002694,31004940,10000003,31001437,31001452,31000690,31001336,31001413,31005119,31001363,31001324,31001514,31001351,31001348,31000714,31001502,31001387,31001449,31001375,31001526,31001399,10000004,31001488,31001831,31001944,31001918,31001615,31001591,31001969,31001538,31001971,31001540,31001476,31001553,31001565,31001755,31001767,31001906,31005712,31001932,31001577,31001589,10000005,31000587,31000599,31005120,31001742,31001730,31001856,31000815,31000841,31000789,31000803,31002011,10000006,31000563,31000866,31000827,31000613,31000839,31000740,31000637,31005404,31000753,31000649,31005415,31000892,31000765,10000007,31001250,31004421,31001019,31001021,31000652,31001274,31001033,31004647,31005324,31000575,31000676,31000791,31004229,31000664,31000296,31001262,10000008,31001084,31001110,31001211,31000942,31004522,31001045,31000955,31004407,31000979,31005095,31001197,31001223,31001058,31001108,31004320,31001286,31000981,31004395,10000009,31005437,31004243,31000928,31000993,31001146,31001173,31001072,31004419,31001161,31000930,31001007,31005426,31001060,31001298,31004357,31004650,31001185,31004369,31004256,10000010,31001692,31001716,31001983,31005368,31001781,31002009,31005723,31001654,31005687,31001666,31001817,31001829,31001995,31005131,31005142,31001870,31002023,31001728,31001882,31001641,31001704,31001779,31001805,31002035,31002047,10000011,31005313,31004306,31004371,31004623,31004700,31004383,31004496,31004546,31004484,31004458,31005790,31004510,31004294,31005084,31004445,31004635,31004217,31004686,31004167,31004609,31004585,31004433,31004270,31004559,31004268,31004282,31004155,31004662,31004460,31004611,10000012,31002100,31005789,31002062,31002249,31005745,31002136,31002225,31002213,31002199,31002163,31002288,31002112,31002175,31005756,31003963,31002201,31002237,31002151,31002187,31005357,31002290,31002074,31002086,31002252,10000013,31003571,31003520,31003569,31003482,31003443,31003532,31003468,31003494,31003431,31003583,31005493,31003518,31003544,31003595,10000014,31003191,31005288,31003215,31003381,31003355,31005302,31003280,31003165,31003367,31003227,31003241,31003429,31003304,31003189,31003177,31003328,31005277,31003393,31003316,31003379,31003203,31003239,31003292,31003417,31005299,10000015,31005507,31003761,31003759,31003658,31003607,31003785,31003672,31003708,31003722,31003746,31003773,31003660,31003684,31003619,31003621,31003696,10000016,31002959,31003088,31002946,31003001,31002934,31002922,31003090,31002997,31003126,31003025,31002961,31003138,31003013,31003076,31003102,31002973,31003052,31002896,31003049,31002910,31003064,31002985,31002908,31003114,10000017,31003987,31004128,31003850,31003886,31003874,31004078,31005108,31003948,31004130,31003924,31004193,31004003,31004015,31004027,31003975,31004054,31004080,31004041,31004142,31005335,31004508,31003811,31003847,31003835,31004066,31003809,31004205,31003898,31004092,31003823,31004116,31004104,31003912,31004039";
        System.out.println("原长度：" + str.length());
        System.out.println("压缩后字符串 size=[" + compress(str).toString().length() + "],str=[" + compress(str).toString() + "]");
        System.out.println("解压缩后字符串：" + StringUtils.newStringUtf8(uncompress(compress(str))));
        System.out.println("解压缩后字符串长度：" + StringUtils.newStringUtf8(uncompress(compress(str))).length());
        System.out.println("解压缩后字符串：" + uncompressToString(compress(str)));

        try {
            String ss = new String(compress(str), "utf-8");
            System.out.println("压缩后字符串1：" + ss);
            System.out.println("解压缩后字符串1：" + uncompressToString(ss.getBytes("utf-8")));
        } catch (UnsupportedEncodingException var3) {
            var3.printStackTrace();
        }

        System.out.println("解压缩后字符串长度：" + uncompressToString(compress(str)).length());
    }
}
